package com.sj.uabrowser.mvp.menu.view.act;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sj.uabrowser.R;
import com.sj.uabrowser.adp.MyPagerAdapter;
import com.sj.uabrowser.app.StatusBarUtil;
import com.sj.uabrowser.db.FileInfo;
import com.sj.uabrowser.db.ThreadInfo;
import com.sj.uabrowser.db.gen.FileInfoDao;
import com.sj.uabrowser.db.gen.ThreadInfoDao;
import com.sj.uabrowser.framework.BaseActivity;
import com.sj.uabrowser.framework.BasePresenter;
import com.sj.uabrowser.mvp.main.bean.HomeTab;
import com.sj.uabrowser.mvp.main.view.adp.MyCustomPagerAdapter;
import com.sj.uabrowser.mvp.main.view.fragment.NewsFragment;
import com.sj.uabrowser.mvp.menu.view.fragment.DownloadDoneFragment;
import com.sj.uabrowser.mvp.menu.view.fragment.DownloadingFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DownloadAct extends BaseActivity implements TabLayout.BaseOnTabSelectedListener {

    @BindView(R.id.tvEdit)
    public TextView tvEdit;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;


    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitles = {"下载中","已完成"};

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_download;
    }

    @Override
    public void setView() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        StatusBarUtil.setLightMode(this);
        setTopTitle("下载文件");
        tvEdit.setText("管理");
        fragments.add(DownloadingFragment.getInstance(tabTitles[0]));
        fragments.add(DownloadDoneFragment.getInstance(tabTitles[1]));
        for (int i = 0; i < fragments.size(); i++) {
            tabLayout.addTab(tabLayout.newTab());
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(R.layout.custom_tab_item_download);

        }


        tabLayout.setupWithViewPager(viewPager);
        MyCustomPagerAdapter adp = new MyCustomPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adp);
        viewPager.setOffscreenPageLimit(fragments.size());
        for (int i = 0; i < adp.getCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(R.layout.custom_tab_item_download);
            TextView textView = tab.getCustomView().findViewById(R.id.tab_text);
            textView.setText(tabTitles[i]);
            if (i == 0) {
                // 设置第一个tab的TextView是被选择的样式
                setTabItemTextStyle(tab, 15, true);
            } else {
                setTabItemTextStyle(tab, 15, false);
                tab.getCustomView().findViewById(R.id.rl_tab).setBackground(getResources().getDrawable(R.drawable.download_tab_selector));
            }
        }
        tabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void setData() {





    }

    @OnClick(R.id.tvEdit)
    public void onClick(View view) {
        startIconAnim(view);
        Fragment fragment = fragments.get(viewPager.getCurrentItem());
        if(fragment instanceof DownloadingFragment){
            ((DownloadingFragment)fragment).onAdmin();
        }else if(fragment instanceof DownloadDoneFragment){
            ((DownloadDoneFragment)fragment).onAdmin();
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        tab.getCustomView().findViewById(R.id.tab_text).setSelected(true);
        setTabItemTextStyle(tab, 15, true);
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        tab.getCustomView().findViewById(R.id.tab_text).setSelected(false);
        setTabItemTextStyle(tab, 15, false);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    protected void setTabItemTextStyle(TabLayout.Tab tab, int fontSize, boolean isSelected) {
        tab.getCustomView().findViewById(R.id.tab_text).setSelected(isSelected);
        TextView textView = tab.getCustomView().findViewById(R.id.tab_text);
        textView.setTextSize(fontSize);
        if (isSelected) {
            textView.setTextColor(getResources().getColor(R.color.white));
        } else {
            textView.setTextColor(getResources().getColor(R.color.colorPrimary));
        }
        View tabIndicator = tab.getCustomView().findViewById(R.id.tab_indicator);
        if (tabIndicator != null) tabIndicator.setVisibility(isSelected ? View.VISIBLE : View.GONE);
        textView.getPaint().setFakeBoldText(isSelected);
        textView.invalidate();
    }
}
